﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

using sgsBase;

namespace gaClear
{
    public partial class FrmMain : Form
    {
        //protected ClearingQueue m_clearque
	    //Protected m_username As String
		protected ClearingQueue _clearQue = new ClearingQueue();
		public static WebClient CMEWebClient = new WebClient();

		DateTime _lastDate;
		long _processed = 0;

		public string SentData
		{
			set
			{
				// set sent data to a screen field.
			}
		}
		public string ReplyData
		{
			set
			{
				// set sent data to a screen field.
			}
		}

        public FrmMain()
        {
			FrmMain.CMEWebClient.Headers.Add("Content-Type", "text/xml");

            InitializeComponent();
        }

        private void QueueTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
			//if the timer was already stopped, do not process this event
			if (tmClearingCheck.Enabled == false)
				return;
			try
			{
				//disable any checks between 11pm and 6am
				if (DateTime.Now.Hour > 23 || DateTime.Now.Hour < 6)
					return;

				DbInterface dbi = new DbInterface();
				dbi.SpNew("sp_nymx_acct_xr_name"); // run sp to clear up unresolved name data.
				dbi.SpRunNon();

				tmClearingCheck.Stop(); // hold the timer just in case this takes more than 5 seconds
				checkForDate();
				// check for any processes looking to resolve data
				//While m_clearque.CheckForResolvedData()
				//    m_proced += 1
				//End While

				////check for any trades waiting to submit data.
				
				//While m_clearque.CheckForWaitingItems()
				//    m_proced += 1
				//End While

				//// check for any new trades
				//While m_clearque.CheckForNewItems()
				//    m_proced += 1
				//End While

				//lbLastProcessed.Text = m_clearque.LastMsg
				//lbNoProcessed.Text = _processed.ToString

				//m_clearque.LogoffCPCID()


				tmClearingCheck.Start();
			}
			catch (Exception ex)
			{
				Email.SendError(ex);
			}
        }

		private void  checkForDate()
		{
			if (_lastDate == null)
				_lastDate = DateTime.Now;

			if (_lastDate.Day != DateTime.Now.Day)
			{
				_lastDate = DateTime.Now;
				lbLastProcessed.Text = "0";
				_processed = 0;
				reloadProducts();
			}
		}

		private void reloadProducts(bool force = false)
		{
		}
	//    Dim logon As New LogonMsg
	//    '		Dim terms As New TermsMsg

	//    ' do not run this during business hours unless requested
	//    If (DateTime.Now.Hour > 8 And DateTime.Now.Hour < 17) And force = False Then
	//        Return
	//    End If
	//    Dim sql As String
	//    Dim dbi As dbInterface = Globals.dbi
	//    sql = "select nymx_login, nymx_pass from users where UserId=0"

	//    dbi.query(sql)

	//    logon.LogonType = UserReqTypes.Logon
	//    logon.Password = dbi.getval("nymx_pass")
	//    Globals.UserName = dbi.getval("nymx_login")

	//    If logon.Submit() Then
	//        Dim prods As New ProductsMsg

	//        prods.Submit()

	//        'm_clearque.RequestAcctUpdate("CL")
	//        m_clearque.UpdateAllAccts()

	//        'clear out and reload firm data/names
	//        dbi.nonquery("delete from nymx_firm_data")

	//        Dim pm As New PartiesMsg

	//        pm.ReqTyp = "7"
	//        pm.Submit()

	//        pm.ReqTyp = "1"
	//        pm.Submit()

	//        dbi.SetupStoredProc("sp_nymx_acct_xr_name")
	//        dbi.RunStoredProc()

	//        logon.LogonType = UserReqTypes.Logoff

	//        logon.Submit()
	//    End If

	//End Sub
		private void frmMain_Load(object sender, EventArgs e)
		{
			tmClearingCheck.Interval = 5000;
			tmClearingCheck.Start();
			//tmClearingCheck.Enabled = false;
		}

		private void btToggleTimer_Click(object sender, EventArgs e)
		{

			if (tmClearingCheck.Enabled)
			{
				btToggleTimer.Text = "Start Timer";
			}
			else
			{
				btToggleTimer.Text = "Stop Timer";
			}
			tmClearingCheck.Enabled = !tmClearingCheck.Enabled;
		}

		private void btTest_Click(object sender, EventArgs e)
		{
			_clearQue.LogonCPCID();
			_clearQue.LogoffCPCID();
		}
    }
}
